/*
 * File : medication.js
 * Version : 0.1
 * Description : 针对用药记录模块所做的一系列js处理
 * Author : hxin
 * Created :2011/8/4
 * 
 */

//页面加载时显示获符合条件的数据
$(document).ready(function(){
	$("#medication").html("");
	$.getJSON("medication!checkRecords.action",{id:0},function(json){
		$.each(json,function(i){
			if(json[i].drug.id == 9){
				json[i].drug.id = "西药";
			}else if(json[i].drug.id == 8){
				json[i].drug.id = "中药";
			}
			$("#medication").append(
			"<tr>"+"<td>"+json[i].drug.id+"</td>"+
			       "<td>"+json[i].drug.term+"</td>"+
			       "<td>"+"一天"+json[i].frequency+"次"+"</td>"+
			       "<td>"+json[i].singleDose+"ml"+"</td>"+
			       "<td>"+"<a href='#' onclick='javascript:deleteMedication("+json[i].id+")'>"+"删除"+"</a>"+"</td>"+
			"</tr>"       
			);	
		});
	});
});

//删除用药记录
function deleteMedication(id) {
	$("#medication").html("");
	$.getJSON("medication!delete.action",{id:id},function(json){
		$.each(json,function(i){
			if(json[i].drug.id == 9){
				json[i].drug.id = "西药";
			}else if(json[i].drug.id == 8){
				json[i].drug.id = "中药";
			}
			$("#medication").append(
			"<tr>"+"<td>"+json[i].drug.id+"</td>"+
			       "<td>"+json[i].drug.term+"</td>"+
			       "<td>"+"一天"+json[i].frequency+"次"+"</td>"+
			       "<td>"+json[i].singleDose+"ml"+"</td>"+
			       "<td>"+"<a href='#' onclick='javascript:deleteMedication("+json[i].id+")'>"+"删除"+"</a>"+"</td>"+
			"</tr>"       
			);	
		});
	});
}

function save(){
	var form = document.getElementById("medicationInputForm");
	form.action = "medication!save.action";
	form.submit();
	parent.location.href="clinic-record-input.action";
	$("#med").click();
}

//弹出新增用药剂量dialog
var insertClick = function(e) {
	var url = "medication!input.action";
    $("<iframe width='100%',height='100%'/>").attr("src",url)
    .dialog({ 
    autoOpen: true, 
    modal: false, 
    width:400,
    height:310,
    position:"top",
    title: "添加用药记录",
    close:function(){
    	$("#medication").html("");
    	$.getJSON("medication!checkRecords.action",{id:0},function(json){
    		$.each(json,function(i){
    			if(json[i].drug.id == 9){
    				json[i].drug.id = "西药";
    			}else if(json[i].drug.id == 8){
    				json[i].drug.id = "中药";
    			}
    			$("#medication").append(
    			"<tr>"+"<td>"+json[i].drug.id+"</td>"+
    			       "<td>"+json[i].drug.term+"</td>"+
    			       "<td>"+"一天"+json[i].frequency+"次"+"</td>"+
    			       "<td>"+json[i].singleDose+"ml"+"</td>"+
    			       "<td>"+"<a href='#' onclick='javascript:deleteMedication("+json[i].id+")'>"+"删除"+"</a>"+"</td>"+
    			"</tr>"       
    			);	
    		});
    	});	
    }
    });
    e.preventDefault();
};